﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawRectModelLineCmd.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "DrawRectElementBehavior.h"
#include "AddGbmpDynamicPannelUtils.h"
#include "GbmpControlIds.h"
#include "IUserTransaction.h"
#include "GbmpUiPlatformUtils.h"
#include "Coordinate3d.h"
#include "ILine2d.h"
#include "IModelLine.h"
#include "IModelView.h"
#include "IGraphicsElementShape.h"
#include "UiDocumentViewUtils.h"
#include "GcmpActionBase.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpDrawRectModelLineCmd)

GbmpDrawRectModelLineCmd::GbmpDrawRectModelLineCmd()
    : CmdCreateRectElement(ID_CMD_GBMP_DRAW_RECT_MODEL_LINE)
{
}

GbmpDrawRectModelLineCmd::~GbmpDrawRectModelLineCmd()
{
}

bool GbmpDrawRectModelLineCmd::OnExecuteCommand()
{
    return true;
}

void GbmpDrawRectModelLineCmd::SetupRibbonContextualPanel()
{
    AddGbmpDynamicPannelUtils::SetupModelLineCreationContext(m_pDrawRectAction, ID_PAGE_GBMP_MODIFY);
}

OwnerPtr<DrawRectElementBehavior> GbmpDrawRectModelLineCmd::CreateDrawRectElementBehavior()
{
    OwnerPtr<DrawRectElementBehavior> opBehavior = NEW_AS_OWNER_PTR(DrawRectElementBehavior, UiDocumentViewUtils::GetCurrentDocument());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBehavior, L"opBehavior为空",L"GDMPLab",L"2024-03-30");
    opBehavior->SetOnFinishCallback(DrawRectElementBehavior::CreateOnFinishCallback(&GbmpDrawRectModelLineCmd::OnRectFinish, this));
    opBehavior->SetOnPreviewCallback(DrawRectElementBehavior::CreateOnPreviewCallback(&GbmpDrawRectModelLineCmd::OnRectPreview, this));

    return TransferOwnership(opBehavior);
}

bool GbmpDrawRectModelLineCmd::CreatePresetElement(ElementId& elementId)
{
    elementId = ElementId::InvalidID;
    return false;
}

bool GbmpDrawRectModelLineCmd::OnRectFinish(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& cornerPt)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制矩形模型线"));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opTransaction.get(), L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    // 将三维坐标点转化为平面上的二维点
    ElementId planeId = pModelView->GetWorkPlaneId();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(planeId.IsValid(), L"planeId无效",L"GDMPLab",L"2024-03-30");
    Coordinate3d planeCoord = GbmpUiPlatformUtils::GetPlaneCoordinate3d(pDoc, planeId);

    // 根据对角线获取矩形的四个角点
    Vector3d rectCorners[4];
    bool bOk = GbmpUiPlatformUtils::GetRectCorners(pModelView, firstPt, cornerPt, rectCorners);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"获取矩形的四个角点失败",L"GDMPLab",L"2024-03-30");
    // 根据四个角点绘制矩形模型线
    for (int i = 0; i < 4; ++i)
    {
        Vector3d startPt = rectCorners[i];
        Vector3d endPt = rectCorners[(i + 1) % 4];
        Vector2d startPtLocal = planeCoord.GetLocalPointXY(startPt);
        Vector2d endPtLocal = planeCoord.GetLocalPointXY(endPt);

        OwnerPtr<ILine2d> opCurve = ILine2d::Create(startPtLocal, endPtLocal);
        IModelLine* pModelLine = IModelLine::Create(pDoc, planeId, TransferOwnership(opCurve), ElementCreationOptions::Normal);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelLine, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");
    }

    opTransaction->Commit();

    return true;
}

bool GbmpDrawRectModelLineCmd::OnRectPreview(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& pos, OwnerPtr<IGraphicsElementShape>& opPreviewGraphics)
{
    return false;
}
